<?php
/**
 * 尚善福 · 商品模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2022 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com；
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Home\Controller;
use Think\Controller;
use Home\Model\GoodsModel;
use Think\Page;
class GoodsController extends BaseController {
    //初始化程序
    public function _initialize(){
        parent::_initialize();
        //实例化相关表
        $usident = M("user");
        //获取数据
        $uid = session('user_auth.usid');
        $user = $usident->where("`us_uid`='".$uid ."'")->find();
        $this->assign('user',$user);
        $conf = tpCache('sIno');
        $this->assign('conf',$conf);
        $this->assign('cate','active');
    }

    /***************************** 商品分类  ************************************/
    /**
	 +--------------------------------------------------------------------------
	 * 实现获取分类页的功能
	 * @author:XC跃
	 * @date: 2018-10-12
	 +--------------------------------------------------------------------------
	 */
    public function cate(){
        //实例化相关表
        $caident = M('category');
        $caList = $caident->where("`ca_parent`='0' and `ca_show`=1")->order("ca_sort asc")->field('ca_id,ca_show,ca_sort,ca_name')->select();
        foreach($caList as $key=>$val){
            $childList = $caident->where("`ca_parent`='".$val['ca_id']."' and `ca_show`=1")->order("ca_sort asc")->field('ca_id,ca_name,ca_thumb')->select();
            $caList[$key]['child'] = $childList;
        }
//        dump($caList);die;
        $this->assign('calist',$caList);
        $this->display();
    }
    /**
    +--------------------------------------------------------------------------
     * 实现获取分类列表的功能
     * @author:GH
     * @date: 2019-01-10
    +--------------------------------------------------------------------------
     */
    public function category(){
        //实例化相关表
        $goident = M('goods');
        $caident = M('category');
        $caid = I('get.id',0); // 当前分类id
        $cate=$caident->where("ca_id =$caid")->field('ca_parent,ca_id')->find();
        if($cate['ca_parent']=='0'){
            $cate=$caident->where("`ca_parent`='".$caid ."'")->getField('ca_id',true);
            $cate = implode(',',$cate);
            $cate_list = $goident->where("`go_sale`=1 and `go_caid` in (" .$cate.")")->field('go_id,go_name,go_price,go_market,go_sum')->select();
            $this->assign('cate_list',$cate_list);
            $this->display();
        }else{
            $cate_list = $goident->where("`go_sale`=1 and `go_caid`='".$cate['ca_id'] ."'")->field('go_id,go_name,go_price,go_market,go_sum')->select();
            $this->assign('cate_list',$cate_list);
            $this->display();
        }
    }
    /********************************商品列表、详情 ***********************************/
    /**
     +--------------------------------------------------------------------------
     * 实现获取商品列表的功能
     * @author:XC跃
     * @date: 2018-10-15
     +--------------------------------------------------------------------------
     */
    public function goodsList(){
        //实例化相关表
        $goident = M('goods');
        $caident = M('category');
        //筛选数组
        $filter_param = array(); 
        $caid = I('get.id',0); // 当前分类id
        $sort = I('sort','go_new'); // 排序
        $sasc = I('sasc','asc'); // 排序
        $filter_param['id'] = $caid; //加入筛选条件中
        $cat_id_arr = getCateGrandson ($caid);
        if(count($cat_id_arr)>1){
            $arr = $caident->where("ca_parent in (".implode(',',$cat_id_arr).")")->field("ca_id")->select();
            $cat_id_arr = array_column($arr,'ca_id');
            //dump($arr);
        }
        $filter_goods_id = $goident->where("go_sale=1 and go_type=1 and go_caid in (".implode(',',$cat_id_arr).")")->cache(true)->getField("go_id",true);
        $count = count($filter_goods_id);
        $page = new Page($count,5);
        if($count > 0){
            $goods_list = M('goods')->where("go_sale=1 and go_type=1 and go_id in (".implode(',', $filter_goods_id).")")->order("$sort $sasc")->limit($page->firstRow.','.$page->listRows)->select();
        }
        $this->assign('goods_list',$goods_list);
        $this->assign('filter_param',$filter_param); // 筛选条件
        $this->assign('cat_id',$caid);
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('sasc', $sasc == 'asc' ? 'desc' : 'asc');
        if($_GET['is_ajax']){
            $this->display('ajaxGoodsList');
        }else{
            $this->display();
        }
    }
    /**
     +--------------------------------------------------------------------------
     * 实现获取商品详情的功能
     * @author:ZJ
     * @date: 2018-12-25
     +--------------------------------------------------------------------------
     */
    public function details(){
        $goid = I("get.id/d");
        //实例化相关表
        $goident = M('goods');
        $giident = M('goods_images');
        //获取商品信息
        $goFind = $goident->where("go_sale=1 and go_id = $goid")->field('go_id,go_name,go_free,go_price,go_market,go_weight,go_sn,go_body,go_score,go_color,go_size')->find();
        if(empty($goFind)){
            $this->error('此商品不存在或者已下架',U('Goods/goodsList'),'2');
        }
        //获取商品缩略图
        $giList = $giident->where("`im_gid`='".$goFind['go_id']."' and `im_type`=1")->order("im_sort")->select();
        //规格属性

        $colorList= explode(",", $goFind['go_color']);
        $sizeList = explode(",", $goFind['go_size']);
//        dump($sizeList);
        //添加历史记录
        $hiident = M('history');
        $usid = session('user_auth.usid');
        $date['hi_uid'] = $usid;
        $date['hi_gid'] = $goid;
        $date['hi_time'] = time();
        $date['hi_type'] = 1;
        $hiident->add($date);
        //dump($giList);
        $this->assign('goods',$goFind);
        $this->assign('goimg',$giList);
        $this->assign("sizeList", $sizeList);
        $this->assign("colorList", $colorList);
        $this->assign('shop',array('phone'=>tpCache('sBas.shoptel')));
        $this->display();
    }
    /**
     +--------------------------------------------------------------------------
     * 实现商品加入收藏的功能
     * @author:XC跃
     * @date :2018-06-07
     +--------------------------------------------------------------------------
     */
    public function addColl(){
        $usid = session('user_auth.usid');
        $goid = trim(I('post.goid/d'));
        //实例化相关表
        $coident = M('collect');
        //判断是否登录
        if(!isset($usid) || empty($usid)){
            exit(json_encode(array('status'=>0,'url'=>U('Login/login'))));
        }
        //获取收藏表信息
        $gcFind = $coident->where("`gc_gid`='".$goid."' and `gc_uid`='".$usid."'")->find();
        if(count($gcFind)>0){
            exit(json_encode(array('status'=>-1,'msg'=>'您已收藏')));
        }
        //添加收藏记录
        $data['gc_uid'] = $usid;
        $data['gc_gid'] = $goid;
        $data['gc_time']= time();
        $data['gc_type']= '1';
        $cores = $coident->add($data);
        if($cores){
            exit(json_encode(array('status'=>1,'msg'=>'收藏成功')));
        }else{
            exit(json_encode(array('status'=>-1,'msg'=>'收藏失败')));
        }
    }


    public function pointsList(){
        //实例化相关表
        $goident = M('goods');
        $caident = M('category');
        //实例化相关Model
        $List = $goident->where("go_sale=1 and go_type = 2")->select();
        $this->assign('list',$List);
        $this->display();
    }

}